﻿//#define WHEEL_DBG

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Cache;
using System.IO;
using System.Xml;

namespace dealfun_fake_wheel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var request = HttpWebRequest.Create(new Uri("http://www.dealfun.com/dealwheel/wheel.xml")) as HttpWebRequest;

            request.Method = "GET";
            request.CookieContainer = cookieJar;

            request.Referer = "http://www.dealfun.com/";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            XmlReader reader =
                 XmlReader.Create(response.GetResponseStream());

            string title = "";
            string id = "";
            bool selected = false;
            while (reader.Read())
            {
                if (reader.NodeType.Equals(XmlNodeType.Element))
                {
                    if(reader.Name.Equals("title"))
                        title = reader.ReadString();
                    else if(reader.Name.Equals("id"))
                        id = reader.ReadString();
                    else if (reader.Name.Equals("type") && reader.ReadString().Equals("mystery"))
                        selected = true;
                } else if(reader.NodeType.Equals(XmlNodeType.EndElement) && 
                    reader.Name.Equals("item")){
                        int index = prize.Items.Add(new KeyValuePair(id, title));
                        if (selected)
                            prize.SelectedIndex = index;
                        selected = false;
                }
            }
            object obj = prize.SelectedItem;
            prize.Sorted = true;
            prize.SelectedItem = obj;
        }
        
        CookieContainer cookieJar = new CookieContainer();
        bool loggedin = false;

        private void login_Click(object sender, EventArgs e)
        {
            login.Enabled = false;


            var request = HttpWebRequest.Create(new Uri("http://www.dealfun.com/login.html")) as HttpWebRequest;
            string str = "email=" + user.Text + "&password=" + password.Text;

            request.Method = "POST";
            request.CookieContainer = cookieJar;

            // turn our request string into a byte stream
            byte[] postBytes = Encoding.ASCII.GetBytes(str);

            // this is important - make sure you specify type this way
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postBytes.Length;
            request.Referer = "http://www.dealfun.com/";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
            request.Expect = null;
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.Headers.Add("Accept-Language", "en-US,en;q=0.8");
            request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
            Stream requestStream = request.GetRequestStream();

            // now send it
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            loggedin = true;
            login.Enabled = true;
            win.Enabled = true;
        }

        public class KeyValuePair
        {

            public object Key;
            public string Value;

            public KeyValuePair(object NewValue, string NewDescription)
            {
                Key = NewValue;
                Value = NewDescription;
            }

            public override string ToString()
            {
                return Value;
            }
        } 

        private void win_Click(object sender, EventArgs e)
        {
            if (loggedin == false)
            {
                MessageBox.Show("You are not logged in. Enter your user info and try again.", "Not logged in", MessageBoxButtons.OK);
            }
            else
            {
                var request1 = HttpWebRequest.Create(new Uri("http://www.dealfun.com/dealwheel/")) as HttpWebRequest;

                request1.Method = "GET";
                request1.CookieContainer = cookieJar;

                request1.Referer = "http://www.dealfun.com/";
                request1.UserAgent = "Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";

                HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
                XmlReader reader1 =
                     XmlReader.Create(response1.GetResponseStream());

                //Don't send spins to dealfun while I'm debugging
                string SPIN_URL;
                #if WHEEL_DBG
                SPIN_URL = "http://www.google.com/dealwheel/spin.php";
                #else
                SPIN_URL = "http://www.dealfun.com/dealwheel/spin.php";
                #endif
                login.Enabled = false;
                win.Enabled = false;
                //----------------------------------------

                var request = HttpWebRequest.Create(new Uri(SPIN_URL)) as HttpWebRequest;
                string str = "prize%5Fid=" + ((KeyValuePair)prize.SelectedItem).Key;

                request.Method = "POST";
                request.CookieContainer = cookieJar;

                // turn our request string into a byte stream
                byte[] postBytes = Encoding.ASCII.GetBytes(str);

                // this is important - make sure you specify type this way
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postBytes.Length;
                request.Referer = "http://www.dealfun.com/assets/swf/deal_wheel.swf";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
                request.Expect = null;
                Stream requestStream = request.GetRequestStream();

                // now send it
                requestStream.Write(postBytes, 0, postBytes.Length);
                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                login.Enabled = true;
                win.Enabled = true;
            }

        }
    }
}
